# Python3 program to find solution of  
# Josephus problem when size of step is 2. 

# Returns position of the k-th killed person
# among a circle of n persons and every second 
# person being killed 
def josephus(n, k): 
      
    # Find value of 2 ^ (1 + floor(Log n)) 
    # which is a power of 2 whose value 
    # is just above n. 
    p = 1
    for _ in range(k):
        p *= 2
  
    # Return 2n - 2^(1 + floor(Logn)) + 1 
    return (2 * n) - p + 1
  
# Driver Code 
tc = int(input())
for _t in range(tc):
    n, k = map(int, input().strip().split(' '))
    print(josephus(n, k))
  
# This code is contributed by Shreyanshi Arun. 
